package com.scoresystem.web.service;

import java.util.List;

import com.scoresystem.common.CommonValue;
import com.scoresystem.dao.DaoFactory;
import com.scoresystem.dao.ICourseDao;
import com.scoresystem.dao.IStudentDao;
import com.scoresystem.dao.ISubjectDao;
import com.scoresystem.dao.ITeacherDao;
import com.scoresystem.dao.IUserDao;
import com.scoresystem.json.JsonContext;
import com.scoresystem.json.JsonState;
import com.scoresystem.model.Course;
import com.scoresystem.model.Student;
import com.scoresystem.model.Subject;
import com.scoresystem.model.Teacher;
import com.scoresystem.model.User;
import com.scoresystem.utils.NumberUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author Administrator
 *处理管理员录入职工学生信息
 */
public class AdminService {
	private DaoFactory factory=DaoFactory.getInstance();
	//维护json的状态类
	private JsonState state=new JsonState();
	//根据不同的json状态选择不同的处理方法
	private JsonContext context=new JsonContext(state);
	
	public JSONObject checkoutInsertstu(Student student){
		IStudentDao studao=factory.createStudentDao();
		IUserDao userdao=factory.createUserDao();
		User user=new User();
		user.setUsername(student.getStunum());
		user.setName(student.getStuname());
		user.setPost("student");
		user.setPassword(student.getPassword());
		try{
			if (!NumberUtil.dealStuNum(student.getStunum(), student.getStuclass())) {
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("请正确输入学号", null, null);
			}
			if (userdao.add(user) && studao.add(student)) {
				studao = factory.createStudentDao();
				JSONArray arr = new JSONArray();
				arr.add(studao.getClasses().toArray());
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("录入成功", null, arr);
			} else {
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("用户名或密码不正确", null, null);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return new JSONObject();
	}
	
	public JSONObject checkoutInsertTea(String[] subjects,Teacher teacher){
		try{
			ITeacherDao tdao=factory.createTeacherDao();
			
			IUserDao udao=factory.createUserDao();
			User user=new User();
			user.setUsername(teacher.getWorknum());
			user.setPost("teacher");
			user.setName(teacher.getTeaname());
			user.setPassword(teacher.getPassword());
			if(udao.add(user)&&tdao.add(teacher)){
				for(String str:subjects){
					ICourseDao cdao=factory.createCourseDao();
					Course course=new Course();
					course.setCoursename(str);
					course.setTeanum(teacher.getWorknum());
					cdao.add(course);
				}
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("录入成功", null, null);
			}
			else{
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("录入失败", null, null);
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return new JSONObject();
	}
	
	public  JSONObject checkoutUpdatestu(Student student){
		IStudentDao studao=factory.createStudentDao();
		try{
			if(studao.findbyName(student.getStunum())==null){
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("用户不存在", null, null);
			}
			studao=factory.createStudentDao();
			if(studao.modify(student)){
				studao = factory.createStudentDao();
				JSONArray arr = new JSONArray();
				arr.add(studao.getClasses().toArray());
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("修改成功", null, arr);
			}
			else{
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("修改失败", null, null);
			}
				
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return new JSONObject();
	}
	
	public JSONObject checkoutquery(String major,String stuclass){
			IStudentDao dao=factory.createStudentDao();
			List<Student> list=dao.listbyMajorandClass(major, stuclass);
			try{
				if(list==null){
					state.setState(CommonValue.STATE_FAILED);
					return context.getObject("数据库异常", null, null);
				}
				else{
					JSONArray jsonArr = JSONArray.fromObject(list);
					state.setState(CommonValue.STATE_SUCCESS);
					return context.getObject("查询成功", null,jsonArr);
				}
			}
			catch(Exception e){
				e.printStackTrace();
			}
			return new JSONObject();
		
	}
	
	public JSONObject checkoutDel(String []stus){
		IStudentDao dao=factory.createStudentDao();
		IUserDao udao=factory.createUserDao();
		try{
			if(dao.deleteStudents(stus)&&udao.deleteUsers(stus)){
				state.setState(CommonValue.STATE_SUCCESS);
				return context.getObject("删除成功", null, null);
			}
			else{
				state.setState(CommonValue.STATE_FAILED);
				return context.getObject("数据库异常", null,null);
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return new JSONObject();
	}
}
